package com.student.liu.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.student.liu.entity.Desk;
import com.student.liu.service.ITableService;
import lombok.Data;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.Map;

/**
 * 商品 Controller
 */
@RestController
@RefreshScope
@RequestMapping("table")
public class TableController extends BaseController {

    @Resource
    ITableService goodsService;

    @PostMapping("page")
    public Object page(@Valid @RequestBody TableController.GoodsPageBody goodsPageBody , BindingResult result) throws InterruptedException {

        IPage<Map<String, Object>> goodsMaps = goodsService.goodsPage(goodsPageBody.getPageIndex(), 15,
                (byte)3);
        return success("", goodsMaps);
    }

    /**
     * 创建一个新商品
     *
     * @param goods
     * @return
     */
    @PostMapping("create")
    public Object create(@RequestBody Desk goods) {
        goodsService.createGoods(goods);
        return success("创建成功", null);
    }

    /**
     * 上架一个商品
     *
     * @return
     */
    @GetMapping("putOn/{id}")
    public Object putOn(@PathVariable Integer id) {
        goodsService.putOn(id);
        return success("上架成功", null);
    }

    /**
     * 获取一个商品
     *
     * @return
     */
    @GetMapping("getById/{id}")
    public Object getById(@PathVariable Integer id) {
        return success(null, goodsService.getById(id));
    }

    /**
     * 下架一个商品
     *
     * @return
     */
    @GetMapping("putDown/{id}")
    public Object putDown(@PathVariable Integer id) {
        goodsService.putDown(id);
        return success("更改为为暂未使用", null);
    }

    @Data
    static class GoodsPageBody {
        @NotNull(message = "请传递页码")
        private Integer pageIndex;
    }



}
